package com.egoo.ticket.server.utils.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：victor_tang
 * @Date: 2019/10/7
 * @Description: 树型数据处理工具类
 */
@Component
@Slf4j
public class TreeDataHandleUtil {
	/**
	 * TODO 将Interactive数据转化为树型结构
	 * @author: victor_tang
	 * @createtime: 2020/1/17 14:05
	 * @param sourceDataList 源数据
	 * @return: java.utils.List<com.egoo.ews.service.vo.InteractiveData>
	 */
	public List<InteractiveDataVo> transInteractiveToTreeData(List<InteractiveDataVo> sourceDataList){
		List<InteractiveDataVo> nodeList = new ArrayList<>();
		for (InteractiveDataVo interactiveData : sourceDataList) {
			if (interactiveData.getParentValue()==null){
				//表明是一级父类
				nodeList.add(interactiveData);
			}
			interactiveData.setChildren(setChild(interactiveData.getValue()+"",sourceDataList));
		}
		return nodeList;
	}
	/**
	 * TODO 设置子树
	 * @author: victor_tang
	 * @createtime: 2020/1/17 14:05
	 * @param value 1
	 * @param sourceDataList 源数据
	 * @return: java.utils.List<com.egoo.ews.service.vo.InteractiveData>
	 */
	private List<InteractiveDataVo> setChild(String value, List<InteractiveDataVo> sourceDataList ){
		List<InteractiveDataVo> childList = new ArrayList<>();
		for (InteractiveDataVo interactiveData : sourceDataList) {
			if (value.equals(interactiveData.getParentValue())){
				childList.add(interactiveData);
			}
		}
		for (InteractiveDataVo interactiveData : childList) {
			interactiveData.setChildren(setChild(interactiveData.getValue()+"",
					sourceDataList));
		}
		if (childList.size()==0){
			return null;
		}
		return childList;
	}


	/**
	 * TODO 将JSONArray带有父子关系的数据转化为树型结构
	 * @author: victor_tang
	 * @createtime: 2020/2/24 19:01
	 * @param rootArray json源数据
	 * @param parentCode 父级编码
	 * @param rootValue 根节点值
	 * @param id 主键id
	 * @param childrenCode 设置子节点变量名
	 * @return: com.alibaba.fastjson.JSONArray
	 */
	public JSONArray transJsonArrayToTreeData(JSONArray rootArray, String parentCode,
								   String rootValue, String id, String childrenCode){
		JSONArray nodeList = new JSONArray();
		for (int i = 0; i < rootArray.size(); i++) {
			JSONObject jsonObject= rootArray.getJSONObject(i);
			if (rootValue.equals(jsonObject.getString(parentCode))) {
				//父节点编码是根节点编码，表明是一级
				nodeList.add(jsonObject);
			}else{
				continue;
			}
			// 设置子树
			JSONArray childJsonArray = setChildList(jsonObject.getString(id),
					rootArray, parentCode,id);
			jsonObject.put(childrenCode,childJsonArray);
		}
		return nodeList;
	}
	/**
	 * TODO
	 * @author: victor_tang
	 * @createtime: 2020/2/24 19:05
	 * @param value id的值
	 * @param rootArray 源数据
	 * @param parentCode 父编码
	 * @param id id编码
	 * @return: com.alibaba.fastjson.JSONArray
	 */
	private JSONArray setChildList(String value, JSONArray rootArray ,
								   String parentCode,String id){
		JSONArray childList = new JSONArray();
		for (int i = 0; i < rootArray.size(); i++) {
			JSONObject jsonObject = rootArray.getJSONObject(i);
			if (value.equals(jsonObject.getString(parentCode))) {
				childList.add(jsonObject);
			}
		}
		for (int i = 0; i < childList.size(); i++) {
			JSONObject jsonObject = childList.getJSONObject(i);
			if (jsonObject!=null) {
				setChildList(jsonObject.getString(id), rootArray,parentCode,id);
			}
		}
		if (childList.size()==0){
			return null;
		}
		return childList;
	}


}
